/*
 * RSS Beas.
 */
package org.nameless.tools.spellcheck.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import org.nameless.tools.spellcheck.Dictionary;
import org.nameless.tools.spellcheck.SpellChecker;
import org.nameless.tools.spellcheck.SpellingErrorAdapter;

/**
 * Main class for the spell-checker tool.
 * @author  bsodhi
 */
public class SpellCheckerApp extends javax.swing.JFrame {

    private Logger logger = Logger.getLogger(getClass().getName());

    /** Creates new form NewJFrame */
    public SpellCheckerApp() {
        initComponents();
        //this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        this.setSize(800, 600);
        initSpellChecker();
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabInputFile = new javax.swing.JLabel();
        jLabSpellErrors = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jLabErrorCount = new javax.swing.JLabel();
        jLabCount = new javax.swing.JLabel();
        dictionarySettingsBtn = new javax.swing.JButton();
        saveResultsBtn = new javax.swing.JButton();
        addToDictionaryBtn = new javax.swing.JButton();
        selectInputBtn = new javax.swing.JButton();
        checkSpellingBtn = new javax.swing.JButton();
        jLabHelp = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Spell Checker Tool");
        setIconImage(getCustomIconImage());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(SpellCheckerApp.class);
        jLabInputFile.setText(resourceMap.getString("jLabInputFile.text")); // NOI18N

        jLabSpellErrors.setText(resourceMap.getString("jLabSpellErrors.text")); // NOI18N

        jTextField1.setToolTipText(resourceMap.getString("jTextField1.toolTipText")); // NOI18N
        jTextField1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextField1CaretUpdate(evt);
            }
        });

        jEditorPane1.setEditable(false);
        jEditorPane1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jEditorPane1.setToolTipText(resourceMap.getString("jEditorPane1.toolTipText")); // NOI18N
        jEditorPane1.setAutoscrolls(false);
        jEditorPane1.setDoubleBuffered(true);
        jEditorPane1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jEditorPane1CaretUpdate(evt);
            }
        });
        jScrollPane1.setViewportView(jEditorPane1);

        jLabErrorCount.setText(resourceMap.getString("jLabErrorCount.text")); // NOI18N

        jLabCount.setText("0");

        dictionarySettingsBtn.setText(resourceMap.getString("dictionarySettingsBtn.text")); // NOI18N
        dictionarySettingsBtn.setToolTipText(resourceMap.getString("dictionarySettingsBtn.toolTipText")); // NOI18N
        dictionarySettingsBtn.setRolloverEnabled(true);
        dictionarySettingsBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dictionarySettingsBtnActionPerformed(evt);
            }
        });

        saveResultsBtn.setText(resourceMap.getString("saveResultsBtn.text")); // NOI18N
        saveResultsBtn.setToolTipText(resourceMap.getString("saveResultsBtn.toolTipText")); // NOI18N
        saveResultsBtn.setEnabled(false);
        saveResultsBtn.setFocusable(false);
        saveResultsBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveResultsBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveResultsBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveResultsBtnActionPerformed(evt);
            }
        });

        addToDictionaryBtn.setText(resourceMap.getString("addToDictionaryBtn.text")); // NOI18N
        addToDictionaryBtn.setToolTipText(resourceMap.getString("addToDictionaryBtn.toolTipText")); // NOI18N
        addToDictionaryBtn.setEnabled(false);
        addToDictionaryBtn.setFocusable(false);
        addToDictionaryBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addToDictionaryBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addToDictionaryBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToDictionaryBtnActionPerformed(evt);
            }
        });

        selectInputBtn.setText(resourceMap.getString("selectInputBtn.text")); // NOI18N
        selectInputBtn.setToolTipText(resourceMap.getString("selectInputBtn.toolTipText")); // NOI18N
        selectInputBtn.setFocusable(false);
        selectInputBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        selectInputBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        selectInputBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectInputBtnActionPerformed(evt);
            }
        });

        checkSpellingBtn.setText(resourceMap.getString("checkSpellingBtn.text")); // NOI18N
        checkSpellingBtn.setToolTipText(resourceMap.getString("checkSpellingBtn.toolTipText")); // NOI18N
        checkSpellingBtn.setEnabled(false);
        checkSpellingBtn.setFocusable(false);
        checkSpellingBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        checkSpellingBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        checkSpellingBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkSpellingBtnActionPerformed(evt);
            }
        });

        jLabHelp.setFont(new java.awt.Font("Arial Black", 1, 12));
        jLabHelp.setText("(?)");
        jLabHelp.setToolTipText(resourceMap.getString("jLabel5.toolTipText")); // NOI18N
        jLabHelp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabHelpMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabHelpMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabHelpMouseExited(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabHelp)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(25, 25, 25)
                                        .add(jLabInputFile))
                                    .add(jLabSpellErrors))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jScrollPane1)
                                    .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)))
                            .add(layout.createSequentialGroup()
                                .add(34, 34, 34)
                                .add(jLabErrorCount)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabCount)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(selectInputBtn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 159, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(checkSpellingBtn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 159, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(addToDictionaryBtn)
                            .add(saveResultsBtn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 159, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(dictionarySettingsBtn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 159, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[] {addToDictionaryBtn, checkSpellingBtn, dictionarySettingsBtn, saveResultsBtn, selectInputBtn}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(14, 14, 14)
                .add(jLabHelp)
                .add(4, 4, 4)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabInputFile)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabSpellErrors)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabCount)
                            .add(jLabErrorCount)))
                    .add(layout.createSequentialGroup()
                        .add(selectInputBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(checkSpellingBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addToDictionaryBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(saveResultsBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(dictionarySettingsBtn)))
                .add(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jTextField1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextField1CaretUpdate
        // Enables/disables the 'Check spellings' button
        String txt = jTextField1.getText();
        checkSpellingBtn.setEnabled((txt != null && txt.trim().length() > 0));
    }//GEN-LAST:event_jTextField1CaretUpdate

    private void jEditorPane1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jEditorPane1CaretUpdate
        // Enables/disables the 'Add selected to ...' button
        String sel = jEditorPane1.getSelectedText();
        addToDictionaryBtn.setEnabled((sel != null && sel.trim().length() > 0));
    }//GEN-LAST:event_jEditorPane1CaretUpdate

    private void dictionarySettingsBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dictionarySettingsBtnActionPerformed
        // Shows dictionary settings screen.
        final JDialog dialog = new JDialog(this, "Dictionary settings", true);
        dialog.setFocusable(true);
        dialog.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    dialog.setVisible(false);
                }
            }
        });
        dialog.getContentPane().add(new Settings(dialog));
        dialog.pack();
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_dictionarySettingsBtnActionPerformed

    private void saveResultsBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveResultsBtnActionPerformed
        //Save the results to a file.
        JFileChooser fc = new JFileChooser(PrefsHelper.getLastAccessedDirectory());
        if (JFileChooser.APPROVE_OPTION == fc.showSaveDialog(this)) {
            File file = fc.getSelectedFile();
            if (file.exists()) {
                int res = JOptionPane.showConfirmDialog(this, 
                    "File already exists. Do you want to overwrite it?", 
                    "Choose one", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
                if (JOptionPane.OK_OPTION != res) {
                    return;
                }
            }
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
                fos.write(jEditorPane1.getText().getBytes(Dictionary.CHAR_ENCODING));
                PrefsHelper.setLastAccessedDirectory(file.getAbsolutePath());
            } catch (IOException ioe) {
                JOptionPane.showMessageDialog(this, "Couldn't save the results.\nError: " +
                        ioe.getLocalizedMessage(), "Save error", JOptionPane.ERROR_MESSAGE);
            } finally {
                try {
                    fos.close();
                } catch (Exception ex) {
                }
            }
        }
    }//GEN-LAST:event_saveResultsBtnActionPerformed

    private void addToDictionaryBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToDictionaryBtnActionPerformed
        // Adds the selected word to default dictionary.
        // Get the selected text
        int st = jEditorPane1.getSelectionStart();
        int se = jEditorPane1.getSelectionEnd();
        String sel = jEditorPane1.getSelectedText();

        // We do something only if any non-space text is selected.
        if (sel != null && sel.trim().length() > 0) {

            String defDict = PrefsHelper.getDefaultDictionary();
            /**
             * Check if the default custom dictionary is set. We add the words
             * to default custom dictionary.
             */
            if (!new File(defDict).exists()) {
                JOptionPane.showMessageDialog(this, "Please set the default " +
                        "custom dictionary first!\nClick 'Dictionary settings'", "No custom dictionary found", JOptionPane.WARNING_MESSAGE);
                return;
            }

            try {
                // Add to the default custom dictionary
                spellChecker.addToDefaultCustomDictionary(sel.trim());

                // In the editor, highlight the word that we added to dictionary
                SimpleAttributeSet red = new SimpleAttributeSet();
                StyleConstants.setForeground(red, Color.BLUE);
                Highlighter.HighlightPainter high =
                        new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
                try {
                    jEditorPane1.getHighlighter().addHighlight(st, se, high);
                } catch (BadLocationException ble) {
                }

                // Clear the selection in editor
                jEditorPane1.setSelectionEnd(jEditorPane1.getSelectionStart());

            } catch (IOException ioe) {
                JOptionPane.showMessageDialog(this, "Couldn't add to default dictionary: " +
                        defDict + ".\nError: " + ioe.getMessage(), "Save error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_addToDictionaryBtnActionPerformed

    private void selectInputBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectInputBtnActionPerformed
        // Opens the file chooser dialog for selecting input document.
        JFileChooser fc = new JFileChooser();
        fc.setAcceptAllFileFilterUsed(false);
        fc.addChoosableFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                String name = f.getName().toLowerCase();
                return f.isDirectory() || name.endsWith(".doc") || name.endsWith(".txt")
                        || name.endsWith(".text");
            }

            @Override
            public String getDescription() {
                return "*.doc, *.txt, *.text";
            }
        });
        if (PrefsHelper.getLastAccessedDirectory() != null) {
            fc.setCurrentDirectory(new File(PrefsHelper.getLastAccessedDirectory()));
        }
        if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            jTextField1.setText(fc.getSelectedFile().getAbsolutePath());
            PrefsHelper.setLastAccessedDirectory(fc.getSelectedFile().getParent());
        }
    }//GEN-LAST:event_selectInputBtnActionPerformed

    private void checkSpellingBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkSpellingBtnActionPerformed
        //Performs the spellcheck on the document.

        String errorMsg = null;
        if (jTextField1.getText() == null || !new File(jTextField1.getText()).exists()) {
            JOptionPane.showMessageDialog(this, "Input file not found: " +
                    jTextField1.getText(), "Error", JOptionPane.WARNING_MESSAGE);
        } else {
            this.setCursor(Cursor.WAIT_CURSOR);
            try {
                boolean checkSpellings = true;
                if (jEditorPane1.getText().length() > 0) {
                    int rs = JOptionPane.showConfirmDialog(this,
                            "Discard previous results?", "Choose one", JOptionPane.YES_NO_OPTION);
                    checkSpellings = rs == JOptionPane.OK_OPTION;
                }
                if (checkSpellings) {
                    // Clear the previour errors
                    errorsAdapter.clearErrors();
                    jLabCount.setText("");

                    // Clear the previous contents from editor
                    jEditorPane1.getDocument().remove(0, jEditorPane1.getDocument().getLength());

                    // Invoke the spell checker
                    spellChecker.checkSpelling(jTextField1.getText(), errorsAdapter);
                    // Update the error count label
                    jLabCount.setText("" + errorsAdapter.getCount());
                    // Enable the 'Save results' button
                    saveResultsBtn.setEnabled(errorsAdapter.getCount() > 0);
                }
            } catch (IOException ex) {
                logger.log(Level.SEVERE, null, ex);
                errorMsg = "Error while loading main " +
                        "dictionary.\nPlease check the installation of this application.";

            } catch (BadLocationException ex) {
                logger.log(Level.SEVERE, null, ex);
                errorMsg = "Error while handling request:\n" + ex.getMessage();
            } catch (Exception ex) {
                logger.log(Level.SEVERE, null, ex);
                errorMsg = "Error while procesing request:\n" + ex.getMessage();
            } finally {
                this.setCursor(Cursor.DEFAULT_CURSOR);
            }
            if (errorMsg != null) {
                JOptionPane.showMessageDialog(this, errorMsg,
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_checkSpellingBtnActionPerformed

    private void jLabHelpMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabHelpMouseEntered
        // Help label mouse event
        jLabHelp.setForeground(Color.RED);
        setCursor(Cursor.HAND_CURSOR);
}//GEN-LAST:event_jLabHelpMouseEntered

    private void jLabHelpMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabHelpMouseExited
        // Help label mouse event
        jLabHelp.setForeground(Color.BLACK);
        setCursor(Cursor.DEFAULT_CURSOR);
}//GEN-LAST:event_jLabHelpMouseExited

    private void jLabHelpMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabHelpMouseClicked
        try {
            // Shows the help window
            JDialog d = new JDialog(this, "Help");
            JTextPane help = new JTextPane();
            help.setPage(Thread.currentThread().getContextClassLoader().
                    getResource("org/nameless/tools/spellcheck/ui/resources/help.htm"));
            help.setEditable(false);
            d.setSize(400, 300);
            d.getContentPane().add(new JScrollPane(help));
            d.setVisible(true);
            d.setLocationRelativeTo(this);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
            
    }//GEN-LAST:event_jLabHelpMouseClicked

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected Image getCustomIconImage() {
        return new ImageIcon(Thread.currentThread().getContextClassLoader().
                getResource("org/nameless/tools/spellcheck/ui/resources/spellcheck.jpg"), "").getImage();
    }

    /**
     * Updates the custom dictionaries as used by the spell checker.
     * @throws java.io.IOException
     */
    public void applyCustomDictionaries() throws IOException {

        // First clear the existing custom dictionaries
        spellChecker.clearCustomDictionaries();
        String defDict = PrefsHelper.getDefaultDictionary();
        // Add custom dictionaries if any set via settings
        Iterator it = PrefsHelper.getDictionaries().values().iterator();
        while (it.hasNext()) {
            String file = it.next().toString();
            spellChecker.addCustomDictionary(file, file.equals(defDict));
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws InterruptedException {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SpellCheckerApp().setVisible(true);
            }
        });
    }

    public JTextField getJTextField1() {
        return jTextField1;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addToDictionaryBtn;
    private javax.swing.JButton checkSpellingBtn;
    private javax.swing.JButton dictionarySettingsBtn;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLabel jLabCount;
    private javax.swing.JLabel jLabErrorCount;
    private javax.swing.JLabel jLabHelp;
    private javax.swing.JLabel jLabInputFile;
    private javax.swing.JLabel jLabSpellErrors;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton saveResultsBtn;
    private javax.swing.JButton selectInputBtn;
    // End of variables declaration//GEN-END:variables
    private SpellChecker spellChecker;
    private SpellingErrorAdapter errorsAdapter;

    public void initSpellChecker() {
        try {            
            spellChecker = new SpellChecker();
            errorsAdapter = new SpellingErrorAdapter(jEditorPane1.getDocument());
            applyCustomDictionaries();            
        } catch (IOException ex) {
            Logger.getLogger(SpellCheckerApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
